package yxy.cherry.game.base.redis;

import java.util.HashMap;

import org.redisson.api.RedissonClient;

import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;

public class RedisManager {
	static private RedisManager instance;

	public static RedisManager getInstance() {
		if (instance == null) {
			instance = new RedisManager();
		}
		return instance;
	}

	private HashMap<RedisType, RedisConnection> conns = new HashMap<>();

	private RedisManager() {

	}

	public void addConnection(RedisType type, RedisConnection conn) {
		conns.put(type, conn);
	}

	public RedisConnection getConnection(RedisType type) {
		return conns.get(type);
	}

	public void disconnect() {
		for (RedisConnection conn : conns.values()) {
			conn.disconnect();
		}
	}

	static public RedissonClient client(RedisConnection redis) throws RedisException {
		if (redis.ready()) {
			try {
				return redis.client();
			} catch (Exception e) {
				throw new RedisException(e);
			}
		} else {
			throw new RedisException("无可用Redis连接!");
		}
	}
}
